package com.example.myservlet;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.example.anno.RequestMapping;
import com.example.filter.SaTokenContextFilterForServlet;
import com.example.init.InitTomcat;
import com.example.util.ConfigUtil;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Context;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;

@Slf4j
@RequestMapping("/login")
public class SatokenTestServlet extends HttpServlet {

    static {
        SaTokenConfig saTokenConfig = ConfigUtil.getConfig("satoken", SaTokenConfig.class);
        SaManager.setConfig(saTokenConfig);

//        ServletContext servletContext = InitTomcat.context.getServletContext();
//        servletContext.addFilter("SaTokenContextFilterForServlet", SaTokenContextFilterForServlet.class)
//                .addMappingForUrlPatterns(null, true, "/*");




//        Context ctx = InitTomcat.context;
//        FilterDef filterDef = new FilterDef();
//        filterDef.setFilterName("SaTokenContextFilterForServlet");
//        filterDef.setFilterClass(SaTokenContextFilterForServlet.class.getName());
//        filterDef.setFilter(new SaTokenContextFilterForServlet());
//        ctx.addFilterDef(filterDef);
//
//        // 创建FilterMap
//        FilterMap filterMap = new FilterMap();
//        filterMap.setFilterName("SaTokenContextFilterForServlet");
//        filterMap.addURLPattern("/*");
//        filterMap.setDispatcher(DispatcherType.REQUEST.name());
//        ctx.addFilterMap(filterMap);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        test1();
    }

    private void test1() {
        StpUtil.login(10001);
        SaSession sessionByLoginId = StpUtil.getSessionByLoginId(10001);
        log.info(sessionByLoginId.toString());
    }

}